package com.export.xlsx.park;

import com.csvreader.CsvReader;
import com.export.xlsx.domain.Company;
import com.export.xlsx.domain.CompanyType;
import com.export.xlsx.service.BusinessService;
import com.export.xlsx.service.CompanyTypeService;
import com.export.xlsx.service.impl.BusinessServiceImpl;
import com.export.xlsx.service.impl.CompanyTypeServiceImpl;
import com.export.xlsx.utils.Constants;
import com.export.xlsx.utils.DateUtils;
import com.export.xlsx.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.nio.charset.Charset;
import java.util.Date;

/**
 * 嘉定园区
 *
 * 1. 张江嘉定园企业名单.csv
 *
 * Created by zhangls on 2018/1/15 0015.
 */
@Slf4j
public class JdData {

    BusinessService businessService = new BusinessServiceImpl();
    CompanyTypeService companyTypeService = new CompanyTypeServiceImpl();

    @Test
    public void testData() throws Exception {
        CsvReader r = new CsvReader("D://张江嘉定园企业名单.csv", ',', Charset.forName("UTF-8"));
        System.out.println(r.readHeaders());
        // 逐条读取记录，直至读完

        String parkId = "1005";

        //记录条数
        int dataCount = 0;

        while (r.readRecord()) {

            //读取一行数据中的值

            String creditCode = StringUtils.getStr(r.get(1));//信用代码
            String name = StringUtils.getStr(r.get(2));//企业名称
            String legalPerson = StringUtils.getStr(r.get(3));//企业法人
            String regMon = StringUtils.getStr(r.get(4));//注册资本
            String companyAddr = StringUtils.getStr(r.get(5));//经营地址
            String telNum = StringUtils.getStr(r.get(6));//联系电话
            String runPro = StringUtils.getStr(r.get(7));//一般性经营项目
            String comTypeStr = StringUtils.getStr(r.get(8));// 企业类型
            String establishmentDateStr = StringUtils.getStr(r.get(9));// 成立日期
            String taxAddr = ""; //税务登记地址
            String taxpayerCode = ""; //纳税人识别号
            String recordDate = "";//登记日期
            String regOrg="嘉定区市场监管局"; //登记机关


            //企业类型
            Integer comType = null;
            if(org.apache.commons.lang.StringUtils.isNotBlank(comTypeStr)){
                CompanyType companyType = companyTypeService.queryByName(comTypeStr);
                if(companyType != null){
                    comType = companyType.getCode();
                }
            }


            //成立日期
            String establishmentDateConvertedStr = "";
            if(org.apache.commons.lang.StringUtils.isNotBlank(establishmentDateStr)){
                establishmentDateConvertedStr = StringUtils.convertToDateStr(establishmentDateStr);
            }


            //第一步：查询现有表是否存在该企业
            Company company = new Company();
            company.setC1(name);
            company.setC2(parkId);

            if(null != businessService.findCompanyInfo(company)){
                //更新操作
                businessService.updateInfo(company, telNum, legalPerson, "", companyAddr, taxAddr, "", "", creditCode, taxpayerCode, regMon, runPro, comType, establishmentDateConvertedStr, recordDate, regOrg);
            } else {
                // 新增操作
                businessService.insertInfo(company, telNum, legalPerson, "", companyAddr, taxAddr, "", "", creditCode, taxpayerCode, regMon, runPro, comType, establishmentDateConvertedStr, recordDate, regOrg);
            }



            dataCount++;
        }

        log.debug("本次处理了嘉定园的 {} 条企业信息", dataCount);
        //关闭
        r.close();
    }

}
